// pages/local/local.js
const app = getApp();
var QQMapWX = require('../../utils/qqmap-wx-jssdk');
var qqmapsdk;
const { _get_area } = require('../../utils/xiaohuangyu')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['河南', '河南'],
    multiArray: [
      ['无脊柱动物', '脊柱动物'],
      ['扁性动物', '线形动物', '环节动物', '软体动物', '节肢动物'],
      ['猪肉绦虫', '吸血虫']
    ],
    multiIndex: [0, 0, 0],
    cityArray: [[], []],
    cityIndex: [0, 0],
    cityName: '全国'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    this.initData()
    qqmapsdk = new QQMapWX({
      key: app.globalData.qqmapsdk_key
    });
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (reqs) {//成功后的回调
            that.setData({
              cityName:reqs.result.address_component.city
            })
          }
        })
      }
    }) 
  },
  initData() {
    const that = this
    _get_area({ 'level': 0 }, res => {
      _get_area({ 'province': res[0].id }, reqs => {
        let cityArray = that.data.cityArray
        cityArray[0] = res
        cityArray[1] = reqs
        this.setData({
          cityArray: cityArray
        })
      })
    })
  },
  MultiChange(e) {
    const cityName = this.data.cityArray[1][e.detail.value[1]].name
    this.setData({
      cityIndex: e.detail.value,
      cityName: cityName
    })
  },
  MultiColumnChange(e) {
    const that = this
    let cityArray = that.data.cityArray
    switch (e.detail.column) {
      case 0:
        var id = cityArray[0][e.detail.value].id
        _get_area({ 'province': id }, reqs => {
          cityArray[1] = reqs
          this.setData({
            cityArray: cityArray
          })
        })
        break;
      case 1:
        break;
    }

  },
  RegionChange(e) {
    console.log(e)
  },
  activeDetailTap() {
    wx.navigateTo({
      url: '../home/bargain/bargain',
    })
  },
  voteactiveTap() {
    wx.navigateTo({
      url: '../home/vote/vote',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})